<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/2
 * Time: 14:27
 */

namespace app\home\controller;
use app\home\model\WechatDepartment;
use app\home\model\WechatUser;
use app\home\model\Snapshot;
use think\Request;
/**个人中心前台控制器
 * Class Person
 * @package app\home\controller
 */
class Person extends Base
{
    public function index(){
        //验证游客登录
        $this->checkAnonymous();
        $Model = new WechatUser();
        $uid = session('userId');
        //查询个人信息
        $msg = $Model->where('userid',$uid)->field('headimgurl,name,mobile')->find();
        $this->assign('msg',$msg);
        return $this->fetch();
    }

    //党员身份证
    public function identity(){
        //验证游客登录
        $this->checkAnonymous();
        $Model = new WechatUser();
        $uid = session('userId');
        //查询个人信息
        $msg = $Model->where('userid',$uid)->field('userid,name,gender,birthday,headimgurl,mobile,position,partytime,department')->find();
        //判断性别
        if ($msg['gender']){
            switch ($msg['gender']){
                case 1:
                    $msg['gender'] = '男';
                    break;
                case 2:
                    $msg['gender'] = '女';
                    break;
                case 3:
                    $msg['gender'] = '未知';
                    break;
            }
        }else{
            $msg['gender'] = '未知';
        }
        //查询部门
        if($msg['department']){
            $department = WechatDepartment::where('id',$msg['department'])->field('name')->find();
            $msg['department'] = $department['name'];
        }
        $this->assign('msg',$msg);
        $request = Request::instance() ->domain();
        $this ->assign('request',$request);
        return $this->fetch();
    }

    //我的发布
    public function release(){
        //验证游客登录
        $this->checkAnonymous();
        $uid = session('userId');
        $snapshot = new Snapshot();
        $list = $snapshot->getList('',$uid);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /**
     * 设置头像
     */
    public function setHeader(){
        $this->checkAnonymous();
        $userId = session('userId');
        $header = input('header');
        $map = array(
            'headimgurl' => $header,
        );
        $info = WechatUser::where('userid',$userId)->update($map);
        if($info){
            return $this->success("修改成功");
        }else{
            return $this->error("修改失败");
        }
    }
    /**
     * 临时党员信息
     */
    public function eg() {
        $this->checkAnonymous();
        $id = input('id');
        $user = WechatUser::where('userid',$id)->find();
        switch ($user['gender']) {
            case 0:
                $user['gender'] = "未定义";
                break;
            case 1:
                $user['gender'] = "男";
                break;
            case 2:
                $user['gender'] = "女";
                break;
            default:
                break;
        }
        //查询部门
        if($user['department']){
            $department = WechatDepartment::where('id',$user['department'])->field('name')->find();
            $user['department'] = $department['name'];
        }
        $this->assign('msg',$user);
        return $this->fetch();
    }

    /**
     * 下拉加载
     */
    public function listMore(){
        $uid = session('userId');
        $snapshot = new Snapshot();
        $length = input('length');
        $list = $snapshot->getList($length,$uid);
        if($list) {
            return $this->success("加载更多","",$list);
        }else{
            return $this->error("加载失败");
        }
    }
}